package com.xiaomili.halfsugar.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.ListDetailAdapter;
import com.xiaomili.halfsugar.fragment.ListDetailFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ListDetailActivity extends AppCompatActivity {


    @InjectView(R.id.list_detail_vp)
    ViewPager mListDetailVp;
    private int mSize;
    private List<Fragment> mList;
    private ListDetailAdapter mAdapter;
    private String mId;
    private ArrayList<String> mIdList;
    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_detail);
        ButterKnife.inject(this);
        initData();
        setAdapter();
    }

    //初始化数据
    private void initData() {
        mPosition = getIntent().getIntExtra("position", 0);
        mSize = getIntent().getIntExtra("size", 0);
        mIdList = getIntent().getStringArrayListExtra("idList");
        mList = new ArrayList<>();
        for (int i = 0; i < mSize; i++) {
            ListDetailFragment detailFragment = ListDetailFragment.newInstance(mIdList.get(i));
            mList.add(detailFragment);
        }
    }

    //设置适配器
    private void setAdapter() {
        mAdapter = new ListDetailAdapter(getSupportFragmentManager(), mList);
        mListDetailVp.setAdapter(mAdapter);
        mListDetailVp.setCurrentItem(mPosition);
        mAdapter.notifyDataSetChanged();
    }

}
